package aidturith.files;

import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import aidturith.Constant;
import aidturith.io.Writer;

/**
 * Génère des playlists à partir d'une liste de fichiers.
 * @author Aidturith
 */
public class Playlist {

	public static void build_m3u8_list(Set<File> files, String playlist) throws IOException {
		Set<String> text = new HashSet<String>();

		for(File f : files)
			text.add(f.getAbsolutePath());

		Writer.write_line(text, playlist, false);
	}

	public static void build_wpl_list(Set<File> files, String playlist, String title) throws IOException {
		Set<String> text = new HashSet<String>();

		// write header
		Writer.write_line("<?wpl version=\"1.0\"?>", playlist, false);
		Writer.write_line("<smil>", playlist, true);
		Writer.write_line("\t<head>", playlist, true);
		Writer.write_line("\t\t<meta name=\"IsNetworkFeed\" content=\"FALSE\"/>", playlist, true);
		Writer.write_line("\t\t<meta name=\"Generator\" content=\"Toho Prayer -- "+Constant.VERSION+"\"/>", playlist, true);
		Writer.write_line("\t\t<meta name=\"ItemCount\" content=\""+files.size()+"\"/>", playlist, true);

		Writer.write_line("\t\t<meta name=\""+title+"\"/>", playlist, true);
		Writer.write_line("\t</head>", playlist, true);
		Writer.write_line("\t<body>", playlist, true);
		Writer.write_line("\t\t<seq>", playlist, true);

		for(File f : files)
			text.add("\t\t\t<media src=\""+f.getAbsolutePath()+"\"/>");
		Writer.write_line(text, playlist, true);

		// write footer
		Writer.write_line("\t\t</seq>", playlist, true);
		Writer.write_line("\t</body>", playlist, true);
		Writer.write_line("</smil>", playlist, true);
	}


	// tests
	public static void main(String[] args) {
		ProcessFile fileProcessor;
		try {
			fileProcessor = new ProcessFile("D:\\Musique\\Doujin\\_Comiket 83\\ALiCE'S EMOTiON - EVOLUTION ЯED");
			Playlist.build_m3u8_list(fileProcessor.getList(), "C:\\Users\\Aidturith\\playlist.m3u8");
			Playlist.build_wpl_list(fileProcessor.getList(), "C:\\Users\\Aidturith\\playlist.wpl", "test");
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}

}
